const express = require('express')
const app = express()
const exjwt = require('express-jwt')
app.listen(4000, () => console.log('端口号为4000的服务器启动'))

app.use(express.urlencoded({ extended: true }))

//静态资源
app.use('/uploads', express.static('./uploads'))


//身份验证
app.use(exjwt({
    secret: 'haha',
    algorithms: ['HS256']
}).unless({
    path: ['/api/reguser', '/api/login']
}))


//个人中心、登录注册
const user = require('./router/user')
app.use(user)

//文章类别管理
const categories = require('./router/categories')
app.use(categories)



//文章管理
const articles = require('./router/articles')
app.use(articles)



//错误捕获
app.use((err, req, res, next) => {
    if (err.name === 'UnauthorizedError') {
        res.status(403).send({ status: 403, message: '身份认证失败' })
    }
})